package org.leon;

import org.leon.builder.SqlSessionFactoryBuilder;
import org.leon.factory.SqlSessionFactory;
import org.leon.io.Resource;
import org.leon.mapper.UserMapper;
import org.leon.pojo.User;

import java.io.InputStream;
import java.util.List;

/**
 * Created by leon.L on 2019/12/25.
 * Desc :
 */
public class TestApp {

    public static void main(String[] args) {

        InputStream resourceAsStream = Resource.getResourceAsStream("sqlMapConfig.xml");

        try {
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sessionFactory.openSession();

//            test1(sqlSession);
            test2(sqlSession);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void test1(SqlSession sqlSession) throws Exception {
        User user = new User();
        user.setId(1);
        user.setName("zhang");

        // Object o = sqlSession.selectOne("org.leon.pojo.User.selectOne", user);
        List<Object> list = sqlSession.selectList("org.leon.pojo.User.findAll");

        System.out.println(list);
    }

    /**
     * 使用动态代理
     */
    private static void test2(SqlSession sqlSession) {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.findAll();

        for (User user : users) {
            System.out.println(user);
        }
    }
}
